package entities;

import td.game.Main;
import td.game.MainGamePanel;
import util.Util;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Point;
import android.os.Handler;
import android.text.TextPaint;
import android.view.MotionEvent;

/** Describe a turret point that is in the map on the mobile device screen.
 *  
 * @author Bruno Zumba (22/09/2011)
 *
 */
public class TurretPoint {
	public static final int WIDTH = Main.SCREEN_WIDTH/MainGamePanel.TILES_IN_X; //Width and Height of the turret point
	public static final int HEIGHT = Main.SCREEN_WIDTH/MainGamePanel.TILES_IN_Y;
	
	private Bitmap bmp; //the actual bitmap of the turret point
	private Point pt; //the absolute position this bmp is in the screen
	
	//The relative position of this bmp relative to the position of the background Map
	private Point relativePt; 
	
	private Boolean touched; //indicates weather this bmp is touched
	
	private int player; //The player that commands this turret point
	private Point tile; //The tile (column, row) this turret point is in
	
	private Handler insideHandler;
	
	private TextPaint textPaint;

	public TurretPoint(int player, Bitmap bmp, Point pt, Point tile, Handler insideHandler){
		this.bmp = Util.resizeBitmap(bmp, WIDTH, HEIGHT);
		
		this.pt = new Point(pt.x, pt.y);
		this.tile = new Point(tile.x, tile.y);
		this.insideHandler = insideHandler;
		touched = false;
		this.player = player;

		textPaint = new TextPaint();
		textPaint.setColor(Color.BLACK);
		textPaint.setTextSize(30);
		
	}
	
	public void destroy(){
		this.bmp.recycle();
		this.bmp = null;
		this.pt = null;
		this.tile = null;
		this.insideHandler = null;
		this.relativePt = null;
	}
	
	public Bitmap getBitmap(){
		return bmp;
	}
	public void setBitmap(Bitmap bmp){
		this.bmp = bmp;
	}
	
	public Point getPoint(){
		return pt;
	}
	public void setPoint(Point pt){
		this.pt = pt;
	}
	
	public Boolean isTouched(){
		return touched;
	}
	public void setTouched(Boolean touched){
		this.touched = touched;
	}
	public Point getTile(){
		return tile;
	}
	
	public int getPlayer(){
		return player;
	}
	public void setPlayer(int player){
		this.player = player;
	}
	
	/** Set the position of this turret point relative
	 * to another point (the top left point of the background map).
	 * 
	 * @param p The relative point
	 */
	public void setRelativePoint(Point p){
		relativePt = p;
		
	}
	
	public void draw(Canvas canvas) {
		canvas.drawBitmap(bmp, pt.x+relativePt.x, pt.y+relativePt.y, null);
		canvas.drawText(String.valueOf(player), pt.x+relativePt.x-15, pt.y+relativePt.y+5, textPaint);
	}
	
	public Boolean handleActionDown(MotionEvent ev) {
		//Handle the event based on the Action it executed
		switch (ev.getAction()) {
			case MotionEvent.ACTION_DOWN: //Touched the screen
				//Check weather or not the turret point was touched
				if (ev.getX() >= (pt.x+relativePt.x) && (ev.getX() <= (pt.x+relativePt.x + bmp.getWidth()))) {
					if (ev.getY() >= (pt.y+relativePt.y) && (ev.getY() <= (pt.y+relativePt.y + bmp.getHeight()))) {
						setTouched(true);
						
						insideHandler.obtainMessage(MainGamePanel.TURRET_POINT, this).sendToTarget();
					} else {
						setTouched(false);
					}
				} else {
					setTouched(false);
				}
			break;
			
			case MotionEvent.ACTION_MOVE: //Moved the finger on the screen.
				//Guess won't do nothing
			break;
			
			case MotionEvent.ACTION_UP: //Untouched the screen
				if (touched){
					touched = false;
				}
			break;
		}
		
		return true;
	}
}
